$(function () {
    $("#jqGrid").Grid({
        url: '../sportsreal/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			//{label: '用户id', name: 'did', index: 'did', width: 80},
			{label: '姓名', name: 'cardName', index: 'card_name', width: 80,align:'center'},
			{label: '昵称', name: 'nickname', index: 'nickname', width: 80,align:'center'},
			{label: '手机号', name: 'phone', index: 'phone', width: 80,align:'center'},
			{label: '号码', name: 'cardNo', index: 'card_no', width: 80,align:'center'},
            {label: '身份证正面照', name: 'cardFace', index: 'cardFace', width: 40,
                formatter: function (value) {return transImg(value);}
            },
            {label: '身份证背面照', name: 'cardBack', index: 'cardBack', width: 40,
                formatter: function (value) {return transImg(value);}
            },
            {label: '身份证手持照', name: 'cardHold', index: 'cardHold', width: 40,
                formatter: function (value) {return transImg(value);}
            },
			{label: '一级认证', name: 'levelOne', index: 'level_one', width: 80,align:'center',
				formatter:function (value) {
					if(value==0||value==null){
						return "未提交";
					}else if(value==1){
						return "待审";
					}else if(value==2){
						return "通过";
					}else if(value==3){
						return "失败";
					}
				}
			},
			{label: '二级认证', name: 'levelTwo', index: 'level_two', width: 80,align:'center',
				formatter:function (value) {
					if(value==0||value==null){
						return "未提交";
					}else if(value==1){
						return "待审";
					}else if(value==2){
						return "通过";
					}else if(value==3){
						return "失败";
					}
				}
			},
			{label: '认证时间', name: 'addTime', index: 'add_time', width: 80,align:'center',
				formatter:function (value) {
					return transDate(value, 'yyyy-MM-dd hh:mm:ss');
				}
			},
			{label: '审核时间', name: 'updateTime', index: 'update_time', width: 80,hidden:true,align:'center',
				formatter:function (value) {
					return transDate(value, 'yyyy-MM-dd hh:mm:ss');
				}
			}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
		showmsg:false,
        title: null,
		sportsReal: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
			one:'',
			two:'',
			msg:''
		},
		msg:''
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.sportsReal = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.sportsReal.id == null ? "../sportsreal/save" : "../sportsreal/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.sportsReal),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../sportsreal/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../sportsreal/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.sportsReal = r.sportsReal;
					if(vm.sportsReal.exists==1){
						vm.showmsg = false;
						vm.q.msg = "此身份证号码已在其他账户绑定过!";
					}else{
						vm.showmsg = true;
					}
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'one':vm.q.one,'two':vm.q.two},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
				one:'',
				two:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },handleFormatError: function (file) {
			this.$Notice.warning({
				title: '文件格式不正确',
				desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
			});
		},handleMaxSize: function (file) {
			this.$Notice.warning({
				title: '超出文件大小限制',
				desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
			});
		},handleSuccessFaceImg: function (res, file) {//上传身份证正面照片
			vm.sportsReal.cardFace = file.response.url;
		},eyeImageFaceImg: function () {//预览身份证正面照片
			var url = vm.sportsReal.cardFace;
			eyeImage(url);
		},handleSuccessBackImg: function (res, file) {//上传身份证背面照片
			vm.sportsReal.cardBack = file.response.url;
		},eyeImageBackImg: function () {//预览身份证背面照片
			var url = vm.sportsReal.cardBack;
			eyeImage(url);
		},handleSuccessHoldImg: function (res, file) {//上传身份证背面照片
			vm.sportsReal.cardHold = file.response.url;
		},eyeImageHoldImg: function () {//预览身份证背面照片
			var url = vm.sportsReal.cardHold;
			eyeImage(url);
		}

	}
});